package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.manager.mapper.SysMenuMapper;
import com.atguigu.spzx.manager.service.SysMenuService;
import com.atguigu.spzx.model.entity.system.SysMenu;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.entity.system.SysUserThreadLocalAuthContextUtil;
import com.atguigu.spzx.model.vo.system.SysMenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public List<SysMenu> findNodes() {
        //获取到所有菜单集合
        List<SysMenu> sysMenuList = sysMenuMapper.selectNodes();
        //对menu对象进行分级(需要递归查找子菜单并赋值给父菜单)
        List<SysMenu> sysMenuTree=SysMenuHelper.buildTree(sysMenuList);
        return sysMenuTree;
    }

    @Override
    public void saveSysMenu(SysMenu sysMenu) {
        sysMenuMapper.insertSysMenu(sysMenu);
    }

    @Override
    public void updateSysMenu(SysMenu sysMenu) {
        sysMenuMapper.updateSysMenu(sysMenu);
    }

    @Override
    public void removeSysMenuById(Long menuId) {
        sysMenuMapper.removeSysMenuById(menuId);
    }

    /**
     * 根据SysMenuVo的属性来看,需要多表联查了
     * 这个api是动态显示菜单,动态是根据当前用户的菜单项来动态显示的
     * 当前用户-用户角色-角色菜单 -菜单表   多表联查.
     * @return
     */
    @Override
    public List<SysMenuVo> menus() {
        SysUser user = SysUserThreadLocalAuthContextUtil.get();
        Long userId = user.getId();
        //先把当前用户所有的菜单集合找出来.
        List<SysMenu>menuList = sysMenuMapper.selectMenusByUserId(userId);
        //在用一个帮助类函数,处理封装成符合要求的List<SysMenuVo>
        List<SysMenuVo> sysMenuVoList = SysMenuHelper.buildMenus(menuList);
        return sysMenuVoList;
    }
}
